﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class MeasuringUnit
    {
        #region 封装属性

        private int mMeasuringUnitNum;

        public int MeasuringUnitNum
        {
            get { return mMeasuringUnitNum; }
            set { mMeasuringUnitNum = value; }
        }

        private string mMeasuringUnitName;

        public string MeasuringUnitName
        {
            get { return mMeasuringUnitName; }
            set { mMeasuringUnitName = value; }
        }

        private string mMeasuringUnitSymbol;

        public string MeasuringUnitSymbol
        {
            get { return mMeasuringUnitSymbol; }
            set { mMeasuringUnitSymbol = value; }
        }

        #endregion

        private IMeasuringUnitDAL measuringunitDAL;

        public MeasuringUnit()
        {
            measuringunitDAL = Adapter.GetMeasuringUnit();
        }

        public void FetchObject(IDataReader dr)
        {
            if (dr!=null)
            {
                mMeasuringUnitNum = ConvertDataType.CheckFiledInt(dr["measuring_unit_num"]);
                mMeasuringUnitName = ConvertDataType.CheckFiledString(dr["measuring_unit_name"]);
                mMeasuringUnitSymbol = ConvertDataType.CheckFiledString(dr["measuring_unit_symbol"]);        
            }        
        }

        public DataTable Display()
        {
            return measuringunitDAL.SelectAll();
        }

        public MeasuringUnit GetMeasuringUnit(string name)
        {
            MeasuringUnit obj = new MeasuringUnit();
            IDataReader dr = measuringunitDAL.Select(name);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }

        public bool CreateMeasuringUnit(MeasuringUnit measuringUnit)
        {
            return measuringunitDAL.Insert(measuringUnit.mMeasuringUnitName, measuringUnit.mMeasuringUnitSymbol) > 0 ? true : false;
        }

        public bool SetMeasuringUnit(MeasuringUnit measuringUnit)
        {
            return measuringunitDAL.Update(measuringUnit.mMeasuringUnitNum, measuringUnit.mMeasuringUnitName, measuringUnit.mMeasuringUnitSymbol) > 0 ? true : false;
        }

        public bool RemoveMeasuringUnit(int measuringUnitNum)
        {
            return measuringunitDAL.Delete(measuringUnitNum) > 0 ? true : false;
        }

        public IList<MeasuringUnit> GetAllMeasuringUnit()
        {
            IList<MeasuringUnit> list = new List<MeasuringUnit>();
            IDataReader dr = measuringunitDAL.Select();
            while (dr != null && dr.Read())
            {
                MeasuringUnit measuringUnit= new MeasuringUnit();
                measuringUnit.FetchObject(dr);
                list.Add(measuringUnit);
            }
            if (dr != null) dr.Close();
            return list;
        }
    }
}
